<template>
	<view class="container">
		<view class="top_box" :style="{'paddingTop':topIconDistance+'px'}">
			<view class="top_bar" :style="{'height':statusBarHeight+'px'}">
				<view class="back" @click="$backTo()">
					<u-icon name="arrow-left"></u-icon>
				</view>
				<view>
					订单详情
				</view>
				<view style="width: 60rpx;"> </view>
			</view>
			<view class="order_status">
				<view class="status_box">
					<view class="big">
						<image src="@/static/images/pMine/dfk.png" mode=""></image>
						<text>{{orderInfo._status._title}}</text>
					</view>
					<view class="small">{{orderInfo._status._msg}}</view>
				</view>
				<!-- <view class="btns" v-if="orderInfo._status._type == 0">
					<view class="s_btn pay btn" @click="goPay">去支付</view>
					<view class="s_btn btn" @click="show = true">取消订单</view>
				</view> -->
				<view class="btns" v-if="orderInfo._status._type == 1">
					<view class="btn b_btn" @click="confirmClick">确定配送</view>
				</view>
				<view class="btns" v-if="orderInfo._status._type == 2">
					<view class="btn b_btn" @click="goAddress">确定送达</view>
				</view>
				<!-- <view class="btns" v-if="orderInfo._status._type == 2">
					<view class="btn b_btn">联系客服</view>
				</view>
				<view class="btns" v-if="orderInfo._status._type == 4">
					<view class="btn b_btn">申请售后</view>
				</view> -->

			</view>
		</view>
		<view class="peisong_info">
			<view class="title">
				<view class="order_num">配送信息</view>
			</view>
			<view class="list">
				<view class="tit">收货地址</view>
				<view class="con">
					<view>{{orderInfo.user_address}}</view>
					<view>{{orderInfo.real_name}} {{orderInfo.user_phone}}</view>
				</view>
			</view>
			<view class="list">
				<view class="tit">配送时间</view>
				<view class="con">
					{{orderInfo.delivery_time == '1'?'半小时内送达':orderInfo.delivery_time +'送达'}}
					<!-- <view>{{orderInfo.user_address}}</view>
					<view>{{orderInfo.real_name}} {{orderInfo.user_phone}}</view> -->
				</view>
			</view>
			<view class="list">
				<view class="tit">配送方式</view>
				<view class="con">
					商家配送
					<!-- <view>{{orderInfo.user_address}}</view>
					<view>{{orderInfo.real_name}} {{orderInfo.user_phone}}</view> -->
				</view>
			</view>
		</view>
		<view class="goods_box">
			<view class="title">
				<view class="order_num">商品清单</view>
				<view class="status">{{orderInfo.total_num}}件商品</view>
			</view>
			<view class="content_con">
				<view class="small_info" v-for="(item,index) in orderInfo.cartInfo" :key="index">
					<image :src="item.productInfo.image" mode="aspectFill"></image>
					<view class="infos">
						<view class="name">{{item.productInfo.store_name}}</view>
						<view class="sku">{{item.productInfo.attrInfo.suk}}</view>
						<view class="num_a_wd">
							<view>x{{item.cart_num}}</view>
							<view class="wd_box" v-if="item2.bd_value !== ''">
								<text class="ice" style="margin-right: 10rpx;">冰冻{{item.bdInfo.bd}}</text>|
								<text style="margin-left: 10rpx;">常温{{item.bdInfo.cw}}</text>
							</view>
						</view>
					</view>
					<view class="price_box">
						<view>
							<view class="big_price"><text>￥</text>{{item.productInfo.attrInfo.price}}</view>
							<view class="small_price"><text>￥</text>{{item.productInfo.attrInfo.ot_price}}</view>
						</view>
					</view>
				</view>
			</view>
			<view class="btns">
				<view></view>
				<view class="count_box">
					已优惠：￥{{orderInfo.coupon_price}}　合计：<text>￥{{orderInfo.pay_price}}</text>
				</view>
			</view>
		</view>
		<view class="order_info">
			<view class="order_title">订单信息</view>
			<view class="num">订单编号：{{orderInfo.order_id}}</view>
			<view class="num">下单时间：{{`${orderInfo.add_time_y} ${orderInfo.add_time_h}`}}</view>
			<view class="num">支付方式：微信支付</view>
			<view class="num">发票信息：{{orderInfo.invoice_company?orderInfo.invoice_company:'无'}}</view>
			<view class="num" style="text-align: left;">备注信息：{{orderInfo.mark?orderInfo.mark:'无'}}</view>
		</view>
		<u-modal v-model="show" :content="content" @confirm="confirm"></u-modal>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				orderInfo: null,
				show: false,
				topIconDistance: 0,
				statusBarHeight: 0,
				orderId: 0,
				content: '是否取消该订单'
			}
		},
		onShow() {

		},
		onLoad(opdata) {

			uni.getSystemInfo({
				success: (res) => {
					// 获取手机顶部状态栏的高度
					const statusBarHeight = res.statusBarHeight || 0;

					// 获取导航栏的高度（手机状态栏高度 + 胶囊高度 + 胶囊的上下间距）
					const menuButtonInfo = uni.getMenuButtonBoundingClientRect();
					const navBarHeight = menuButtonInfo.height + (menuButtonInfo.top - statusBarHeight) *
						2;
					// 计算顶部图标距离
					this.topIconDistance = navBarHeight;
					this.statusBarHeight = statusBarHeight;

					// 打印顶部图标距离
				},
				fail: (err) => {
					console.error('获取系统信息失败:', err);
				},
			});
			this.orderId = opdata.order
			this.getData()
		},
		methods: {
			goAddress() {
				this.$gourl('/riderMine/confirmReach?order_id=' + this.orderInfo.order_id)
			},
			confirmClick() {
				uni.showModal({
					title: '提示',
					content: '确定要配送此订单吗？！',
					success: (res) => {
						if (res.confirm) {
							this.$allApi.changeOrderStatus({
								orderid: this.orderInfo.order_id,
								status: 1,
							}).then(res => {
								this.getData()
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			goPay() {
				this.$allApi.submitOrderPay({
					uni: this.orderId,
					from: 'routine'
				}).then(res => {
					let pay = res.data.result.jsConfig
					console.log(pay);
					uni.requestPayment({
						timeStamp: pay.timestamp, // 时间戳（单位：秒）
						nonceStr: pay.nonceStr, // 签名算法，这里用的 MD5/RSA 签名
						package: pay.package, // 固定值
						signType: pay.signType, // 签名算法，这里用的 MD5/RSA 签名
						paySign: pay.paySign, //签名
						// 支付成功的回调
						success: payRes => {
							console.log(payRes)
							if (payRes.errMsg == "requestPayment:ok") {
								uni.showToast({
									icon: "success",
									title: "支付成功！"
								})
								this.getData()
							}

						},
						// 支付失败回调
						fail: err => {
							console.log(err);
							uni.showToast({
								icon: "none",
								title: "支付失败"
							})
						}
					})
				})
			},
			confirmOrder(id) {
				this.$allApi.updateOrderStatus({
					id: this.orderInfo.id,
					status: 4
				}).then(res => {
					this.$u.toast('确认收货成功')
					this.page = 1
					this.getData()
				})
			},
			confirm() {
				this.$allApi.updateOrderStatus({
					id: this.orderInfo.order_id
				}).then(res => {
					this.$u.toast('取消订单成功')
					setTimeout(() => {
						this.$backTo()
					}, 1500)
				})
			},
			getData() {
				this.$allApi.getOrderDetail({
					order: this.orderId
				}).then(res => {
					console.log(res);
					this.orderInfo = res.data
					for (let item of res.data.cartInfo) {
						item.bdInfo = {}
						if (item.bd_value && item.bd_value[0] == '{') {
							item.bdInfo = JSON.parse(item.bd_value)
							console.log(item.bdInfo);
							if (item.bdInfo.type == '全冰') {
								item.bdInfo.bd = item.cart_num * item.productInfo.attrInfo.num
								item.bdInfo.cw = 0
							} else if (item.bdInfo.type == '一半冰一半不冰') {
								item.bdInfo.bd = Math.ceil(item.cart_num * item.productInfo.attrInfo.num / 2)
								item.bdInfo.cw = item.cart_num * item.productInfo.attrInfo.num - item.bdInfo.bd
							} else if (item.bdInfo.type == '常温') {
								item.bdInfo.bd = 0
								item.bdInfo.cw = item.cart_num * item.productInfo.attrInfo.num
							} else if (item.bdInfo.type == '自定义冰冻数量') {
								item.bdInfo.bd = Math.ceil(item.cart_num * item.productInfo.attrInfo.num * item
									.bdInfo.bfNumber)
								item.bdInfo.cw = item.cart_num * item.productInfo.attrInfo.num - item.bdInfo.bd
							}
						} else {
							item.bdInfo = {
								bd: 0,
								cw: 0
							}
						}
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.wd_box {
		display: flex;
		margin-left: 20rpx;
		align-items: center;
		justify-content: space-between;
		background: #F6F6F7;
		height: 32rpx;
		border-radius: 6rpx;
		font-size: 20rpx;
		padding: 0 14rpx;

		text:nth-child(2) {
			padding: 0 14rpx;
		}

		.ice {
			color: #26ACF5;
		}
	}

	.peisong_info {
		width: 710rpx;
		margin: 30rpx auto;
		padding: 20rpx 30rpx;
		background: #fff;
		border-radius: 20rpx;
		margin-top: 60rpx;

		.title {
			padding-bottom: 20rpx;
			position: relative;
			display: flex;
			align-items: center;
			font-size: 28rpx;
			color: #333;
			justify-content: flex-start;
		}

		.list {
			border-top: 1px solid #eee;
			display: flex;
			justify-content: space-between;
			font-size: 24rpx;
			padding: 20rpx 0;

			.tit {
				color: #999;
			}

			.con {
				text-align: right;
			}
		}
	}

	.order_status {
		position: relative;
		padding: 30rpx;
		width: 710rpx;
		margin: 0 auto;
		margin-top: 140rpx;
		height: 280rpx;
		opacity: 1;
		border-radius: 20rpx;
		background: #FFFFFF;
		box-shadow: 0rpx 0rpx 16rpx #ccc;

		.btns {
			padding-top: 20rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.s_btn {
				width: 300rpx;
				height: 58rpx;
			}

			.b_btn {
				width: 100%;
				height: 58rpx;
			}

			.btn {
				border-radius: 10rpx;
				display: flex;
				color: #333;
				align-items: center;
				justify-content: center;
				border: 1rpx solid #999999;
			}

			.pay {
				background: #EA3D37;
				color: #fff;
				border-color: #EA3D37;
			}
		}

		.status_box {
			padding-bottom: 20rpx;
			border-bottom: 1px solid #ccc;

			.big {
				font-size: 36rpx;
				display: flex;
				align-items: center;
				justify-content: flex-start;

				image {
					width: 34rpx;
					height: 34rpx;
					margin-right: 20rpx;
				}
			}

			.small {
				font-size: 24rpx;
				line-height: 2;
			}
		}

	}

	.order_info {
		width: 710rpx;
		border-radius: 20rpx;
		background: #FFFFFF;
		margin: 0 auto;
		padding: 20rpx;

		.num {
			line-height: 1.8;
			font-size: 24rpx;
			color: #333333;
			border-top: 1px solid #eee;
			padding: 20rpx 0;
		}

		.order_title {
			padding-bottom: 20rpx;
		}
	}

	.foot {
		width: 710rpx;
		height: 80rpx;
		border-radius: 10rpx;
		background: #EA3D37;
		left: 20rpx;
		position: fixed;
		bottom: calc(20rpx + constant(safe-area-inset-bottom));
		bottom: calc(20rpx + env(safe-area-inset-bottom));
		display: flex;
		align-items: center;
		justify-content: center;
		color: #fff;
	}

	.foot2 {
		width: 710rpx;
		height: 80rpx;
		background: #FFFFFF;
		border: 2rpx solid #EA3D37;
		border-radius: 10rpx;
		left: 20rpx;
		position: fixed;
		bottom: calc(20rpx + constant(safe-area-inset-bottom));
		bottom: calc(20rpx + env(safe-area-inset-bottom));
		display: flex;
		align-items: center;
		justify-content: center;
		color: #EA3D37;
	}

	.goods_box {
		width: 710rpx;
		margin: 30rpx auto;
		padding: 20rpx 30rpx;
		background: #fff;
		border-radius: 20rpx;

		.btns {
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding-top: 20rpx;

			text {
				font-weight: bold;
				color: #EA3D37;
			}
		}

		.title {
			padding-bottom: 20rpx;
			position: relative;
			border-bottom: 1px solid #eee;
			display: flex;
			align-items: center;
			font-size: 28rpx;
			color: #333;
			justify-content: flex-start;

			.status {
				font-size: 24rpx;
				margin-left: 20rpx;
				color: #999999
			}
		}

		.content_con {
			padding-top: 30rpx;
			padding-bottom: 20rpx;
			border-bottom: 1px solid #eee;

			.head {
				padding: 20rpx 0;
				display: flex;
				align-items: center;
				justify-content: space-between;

				.left {
					display: flex;
					align-items: center;

					image {
						width: 50rpx;
						height: 50rpx;
						border-radius: 10rpx;
						margin-right: 20rpx;
					}
				}
			}

			.small_info {
				margin-top: 20rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;

				image {
					width: 130rpx;
					height: 130rpx;
					border-radius: 10rpx;
				}

				.price_box {
					display: flex;
					align-items: center;
					justify-content: center;
					text-align: right;

					.big_price {
						font-size: 32rpx;

						text {
							font-size: 24rpx;
						}
					}

					.small_price {
						font-size: 28rpx;
						color: #666;
						text-decoration: line-through;

						text {
							font-size: 16rpx;
						}
					}
				}

				.infos {
					flex: 1;
					padding-left: 20rpx;
					width: 596rpx;
					line-height: 1.8;

					.name {
						font-size: 24rpx;
						text-overflow: -o-ellipsis-lastline;
						overflow: hidden;
						text-overflow: ellipsis;
						display: -webkit-box;
						-webkit-line-clamp: 1;
						-webkit-box-orient: vertical;
					}

					.sku {
						font-size: 20rpx;
						color: #666;
					}
				}

				.num_a_wd {
					display: flex;
					align-items: center;
					justify-content: flex-start;
					font-size: 24rpx;

					.wd_box {
						display: flex;
						margin-left: 20rpx;
						align-items: center;
						justify-content: space-between;
						background: #F6F6F7;
						height: 32rpx;
						border-radius: 6rpx;
						font-size: 20rpx;
						padding: 0 14rpx;

						.ice {
							color: #26ACF5;
						}
					}
				}
			}

		}
	}

	.top_bar {
		position: relative;
		width: 100vw;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 0 10rpx;
		color: #fff;

		.back {
			width: 60rpx;
			height: 60rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			font-size: 40rpx;
		}
	}

	.my_order {
		position: relative;
		width: 710rpx;
		margin: 0 auto;
		margin-top: -110rpx;
		background: #fff;
		border-radius: 20rpx;
		height: 160rpx;
		padding: 20rpx;
		padding-top: 30rpx;
		display: flex;
		justify-content: space-between;

		.address {
			flex: 1;
			padding-left: 20rpx;

			.user {
				font-weight: 500;
			}

			.city {
				color: #999999;
				margin-top: 8rpx;
			}
		}
	}

	.top_box {
		height: 564rpx;
		position: relative;
	}

	.top_box:before {
		content: '';
		position: absolute;
		top: 0;
		left: -50%;
		width: 200%;
		height: 100%;
		z-index: 0;
		background-color: #EA3D37;
		/* 背景颜色 */
		border-bottom-left-radius: 50%;
		border-bottom-right-radius: 50%;
	}

	.container {
		width: 750rpx;
		overflow-x: hidden;
		min-height: 100vh;
		background: #F8F8F8;
		padding-bottom: calc(150rpx + env(safe-area-inset-bottom))
	}
</style>